import request from '@/utils/request'

const NAME = 'loadDataAuto';

export default {
    name: NAME,
    formCreateParser: {
        mergeProp(ctx) {
            const props = ctx.prop.props;
            if (!props.nodeKey) props.nodeKey = 'id';
            if (!props.props) props.props = {
                label: 'title'
            };
        }
    },
    props: {
        formCreateInject: {
            type: Object,
            required: true,
        }
    },
    data(){
        return{

        }
    },
    watch: {

    },
    methods: {
        loadData(param){
            console.log('loadDataAuto.loadData() from comp')
            console.log(param)
            //
            const formCreateInject = this.formCreateInject
            const fApi = formCreateInject.api
            let url = formCreateInject.prop.props.url
            url=url?url.trim():''
            let method = formCreateInject.prop.props.method
            method=method?method.trim():'post'
            let paramData = formCreateInject.prop.props.paramData
            const newParam = {}
            Object.assign(newParam, paramData, param)
            // url query里参数，存入store中，所有api调用时，一起发给后端，参数中有token, 可用作身份验证
            if (this.$route&&this.$route.query&&this.$route.query.dynamicFormId) {
                let queryData = {}
                Object.assign(queryData, this.$route.query)
                Object.assign(newParam, {'urlQueryData':queryData})
            }
            // api
            return request({
                url,
                method,
                data: newParam
            }).then(r => {
                console.log(r)
                if (!r.success) {
                    this.notify(r)
                    return;
                }
                const dataList = r.obj
                console.log('dataList')
                console.log(dataList)
                if (dataList.length==0) {
                    return;
                }
                const data = dataList[0]
                fApi.setValue(data)
            })
        }
    },
    render() {
        return (
            <div></div>
        );
    },
    mounted() {

    }
}
